package sanphone.com.runningschool.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.InterfaceLocation;
import sanphone.com.runningschool.MyOrder.AcceptListViewAdapter;
import sanphone.com.runningschool.R;
import sanphone.com.runningschool.RequestHttp.RequestHttp;
import sanphone.com.runningschool.entity.JuShouInfo;
import sanphone.com.runningschool.entity.Order;

import static android.content.Context.MODE_PRIVATE;
// TODO: 2018/8/7 用户接受的订单页面 

public class AcceptedMessageFragment extends Fragment {
    private List<Order> infos = null;
    private ListView accept_listview;
    private AcceptListViewAdapter adapter;
    private String id;
    private SharedPreferences sprf;
    private SwipeRefreshLayout refreshLayout;
    private ForceOffline offline;
    private TextView empty;


    public static AcceptedMessageFragment getInstance() {
        return new AcceptedMessageFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.accepted_message_fragment, null);
        sprf=getActivity().getSharedPreferences("data",MODE_PRIVATE);
        empty=view.findViewById(R.id.acceptd_message_empty);
        id = sprf.getString("schoolId", null);
        offline = new ForceOffline(id,getActivity());
        initView(view);
        refreshLayout.setColorSchemeResources(R.color.colorPrimary);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                refresh();
            }
        });
        return view;
    }

    private void addData() {
        SharedPreferences sprf=getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
        final String idStr=sprf.getString("schoolId",null);
        ExecutorService executorService= Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                RequestHttp requestHttp=new RequestHttp(InterfaceLocation.getLocation()+"showBehaviorByAcceptId?acceptId="+idStr,getActivity());
                try {
                    String response=requestHttp.get();
                    Log.e("jushou", response );
                    String data=new JSONObject(response).getString("data");
                    Gson gson=new Gson();
                    infos=gson.fromJson(data,new TypeToken<List<Order>>(){}.getType());
                    if(infos.size()==0){
                        empty.setVisibility(View.VISIBLE);
                    }
                    Collections.reverse(infos);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter=new AcceptListViewAdapter(getActivity(),infos);
                           accept_listview.setAdapter(adapter);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {

                }

            }
        });
        executorService.shutdown();
    }

    @Override
    public void onResume() {
        super.onResume();
        addData();
        offline.check();
    }

    private void initView(View view) {
        accept_listview = view.findViewById(R.id.accept_listview);
        refreshLayout=view.findViewById(R.id.a_message_refresh);
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        addData();
                        offline.check();
                        refreshLayout.setRefreshing(false);
                    }
                });
            }
        }).start();
    }
}
